package com.itheima;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Set;

@Component
public class ClearImgTask {

    @Autowired
    private JedisPool jedisPool;


    @Scheduled(cron = "0/10 * * * * ?")
    public void clearImg(){

        System.out.println("图片开始清理了....");

        //访问redis获取垃圾图片的名称
        Set<String> smembers = jedisPool.getResource().smembers(RedisConstant.SETMEAL_PIC_RESOURCES);

        //判断是否有垃圾图片
        if(smembers!=null&&smembers.size()>0){
            smembers.stream().forEach(imgName->{
                //调用七牛云的工具类删除垃圾图片
                QiniuUtils.deleteFileFromQiniu(imgName);
                //删除redis中的垃圾图片的名称
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,imgName);
            });
        }




    }
}
